package com.lfy.controller;

import com.lfy.domain.dto.ShareFileDTO;
import com.lfy.domain.dto.ShareFileDownloadDTO;
import com.lfy.domain.dto.WebShareCheckDTO;
import com.lfy.domain.vo.WebFileVO;
import com.lfy.domain.vo.WebShareFileDetailVO;
import com.lfy.domain.vo.WebShareFileVO;
import com.lfy.modules.common.anno.CheckShareToken;
import com.lfy.modules.common.custom.WebShareDownloadVO;
import com.lfy.modules.common.domain.R;
import com.lfy.service.WebDiskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/web/disk")
@CrossOrigin
public class DiskWebController {
    @Resource
    private WebDiskService webDiskService;

    /**
     * 从博客主页获取文件列表
     * @param url 博客uri
     * @return 文件列表
     */
    @RequestMapping("/list")
    public R<List<WebFileVO>> list(@RequestParam("url") String url) {
        return webDiskService.getFileList(url);
    }

    /**
     * 从博客主页下载文件
     * @param url 博客uri
     * @param id 文件id
     * @return 文件下载
     */

/*    public ResponseEntity<byte[]> download(@RequestParam("url") String url,@RequestParam("id") Long id) {
        return webDiskService.download(url,id);
    }    */
    @GetMapping("/download")
    public void download(@RequestParam("url") String url, @RequestParam("id") Long id, HttpServletResponse response) {
         webDiskService.download(url,id,response);
    }
    /**
     * 分享链接
     */
    @GetMapping("/share/query")
    public R<WebShareFileVO> getShareFile(@RequestParam("shareKey") String shareKey, @RequestParam(value = "shareKey",required = false)String shareCode){
        return webDiskService.getShareFile(shareKey,shareCode);
    }
    /**
     * 分享链接提取码校验
     */

    @PostMapping("/share/check")
    public R<String> checkShareCode(@RequestBody @Validated WebShareCheckDTO dto){
        return webDiskService.checkShareCode(dto);
    }
    /**
     * 文件分享-查询文件列表
     */
    @PostMapping("/share/list")
    @CheckShareToken(value = "#dto.fileId",token = "#dto.token",parentId = "#dto.parentId")
    public R<List<WebShareFileDetailVO>> getShareFile(@RequestBody @Validated ShareFileDTO dto){
        return this.webDiskService.getShareFileList(dto);
    }
    /**
     * 批量下载
     */

    @GetMapping("/share/downloads")
    public  R<List<WebShareDownloadVO>> batchDownload(ShareFileDownloadDTO dto){
        String token = dto.getToken();
        List<Long> ids = dto.getIds();

        if(ids == null || ids.size() == 0){
            return R.error("文件id不能为空");
        }
        if(StringUtils.isBlank(token)){
            return R.error("token不能为空");
        }
         return this.webDiskService.batchDownload(ids,token);
    }

    @GetMapping("/share/download")
    public  void downloadToStream(@RequestParam("id") Long id,@RequestParam("token") String token, HttpServletResponse res){
         this.webDiskService.downloadToStream(id,token,res);
    }

    /**
     * 获取分享链接文件id列表以及文件夹下所有文件id
     * @return 文件id列表
     */
    @PostMapping("/share/getNoFolderFilesId")
    public  R<List<Long>> getNoFolderFilesId(@RequestBody  ShareFileDownloadDTO dto){
        String token = dto.getToken();
        List<Long> ids = dto.getIds();

        if(ids == null || ids.size() == 0){
            return R.error("文件id不能为空");
        }
        if(StringUtils.isBlank(token)){
            return R.error("token不能为空");
        }
        return this.webDiskService.getNoFolderFilesId(token,ids);
    }
    /*MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCNTj5ZuSPFJ9HT81lsMTzTSmih+Rb9VhdDSQ
    0rbyrgMe/SXlEFlLAQeDeKilVR4f0zARfuhQ3kplHMDrsYr7d5STvapxdCbhlqv8sEbFdAfvCu0LAhoMxxRe24mO
    5UPmg/E6hueST5N6WKnLY5mGEhLYifQZcPbYpphJsnKieW0rIRwCGjf07Xwe7btIkbpdRJHHUFFWvbwAtPr6oMQc
    7f6A8o/GLBcLlMmnQFMSo8ZX46k6OcqbP4raHYk4fyNAkOCn6/AC7oVl548A0zRyRGwY3pR3QVP54J3EJu9F2UHn
    A4LN+tn9oJsKdK2/3jb9oH3xrx3XeGHhqCj1r6sJN1AgMBAAECggEAATbB3F11zDTKXarCguZt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